import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 言初
 * Date: 2024-05-10
 * Time: 10:36
 */

// 读进程 加锁
public class read {
    private static RandomAccessFile raf;


    public void func(int writenums,int readnums) throws Exception {

        raf = new RandomAccessFile("E:/test.txt", "rw");
        FileChannel fc = raf.getChannel();
        MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, 0, 1024);
        FileLock flock=null;


        for(int i=0;i<36;i++){
            flock=fc.lock();    //上锁
            System.out.println( System.currentTimeMillis() +  ":read1:" + (char)mbb.get(i));
            flock.release();    //释放锁
            Thread.sleep(1000);
        }


    }
}